package com.example.neusoft.yuejian.Base;

import android.support.annotation.NonNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseModel<T> {
   public BaseService service;

   public BaseModel(){
       Retrofit retrofit=new Retrofit.Builder().baseUrl(URL.BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
       service = retrofit.create(BaseService.class);
   }
    public void callEnqueue(Call<T> call, final BaseListener<T> listener) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
                if (response.body() != null && response.isSuccessful())
                    listener.onResponse(response.body());
                else
                    listener.onFail(response.message()+response.code());
            }

            @Override
            public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
                listener.onFail(t.getMessage());
            }
        });
    }
    public void callListEnqueue(Call<List<T>> call,final BaseListener<List<T>> listListener) {
       call.enqueue(new Callback<List<T>>() {
           @Override
           public void onResponse(Call<List<T>> call, Response<List<T>> response) {
               if (response.body() != null && response.isSuccessful())
                   listListener.onResponse(response.body());
               else
                   listListener.onFail(response.message()+response.code());
           }

           @Override
           public void onFailure(Call<List<T>> call, Throwable t) {
               listListener.onFail(t.getMessage());
           }
       });
    }
}
